package api

import (
	"flag"
	"github.com/gin-gonic/gin"
	"net/http"
	"project_api/intrenal/api/request"
	"project_api/intrenal/api/response"
	"project_api/intrenal/handler"
	__ "project_video_srv/proto"
	"strconv"
)

var (
	videoServer = flag.String("videoServer", "127.0.0.1:50052", "The server address")
	videoSer    = handler.NewClient().VideoServerClient(videoServer)
)

func Create(c *gin.Context) {
	var json request.Create
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	value, _ := c.Get("userId")

	userId, _ := strconv.Atoi(value.(string))

	create, err := videoSer.Create(c, &__.CreateRequest{
		UserId:       int64(userId),
		Video:        json.Video,
		VideoTitle:   json.VideoTitle,
		Address:      json.Address,
		RedPkg:       json.RedPkg,
		Sex:          int64(json.Sex),
		Age:          int64(json.Age),
		GiveEveryone: int64(json.GiveEveryOne),
		Where:        json.Where,
		Pkg:          json.Pkg,
	})
	if err != nil {
		response.Response(c, 400, err.Error(), nil)
		return
	}

	response.Response(c, 400, "视频发布成功", create)

}

func List(c *gin.Context) {
	var json request.List
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	list, err := videoSer.List(c, &__.ListRequest{
		Page:     int64(json.Page),
		PageSize: int64(json.PageSize),
		KeyWord:  json.KeyWord,
	})
	if err != nil {
		response.Response(c, 400, err.Error(), nil)
		return
	}

	response.Response(c, 400, "视频展示成功", list)

}
